﻿/*
 *	产品页点击事件
 */

$(document).ready(function () {
    up_addproduct_click();
    up_alterproduct_photo_click();
    up_alterproduct_info_click();
    up_recoverproduct_click();
    up_deleteproduct_click();
});


// 添加产品
function up_addproduct_click() {
    $(".product_add_a").die().live("click", function (e) {
        e.preventDefault();
        var parenthandler = $(this).parent().parent().parent().parent().parent().parent().parent();

        var product_refrences_user_email = $("#ru_g_id").val();
        var product_name = parenthandler.find(".rp_name").val();
        var product_sum = parenthandler.find(".rp_sum").val();
        var product_price = parenthandler.find(".rp_price").val();
        var product_decript = parenthandler.find(".rp_descript").val();


        if (product_refrences_user_email == "") {
            alert("添加失败");
            return;
        }
        if (product_name == "") {
            parenthandler.find(".rp_name").focus();
            return;
        }
        if (product_sum == "") {
            parenthandler.find(".rp_sum").focus();
            return;
        }
        if (product_price == "") {
            parenthandler.find(".rp_price").focus();
            return;
        }
        if (product_decript == "") {
            parenthandler.find(".rp_descript").focus();
            return;
        }

        $.ajax({
            url: "/Mange_Main/AddProduct",
            type: "post",
            cache: false,
            data: {
                product_refrences_user_email: product_refrences_user_email,
                product_name: product_name,
                product_sum: product_sum,
                product_price: product_price,
                product_decript: product_decript
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    var new_product = {}; //联系人集合
                    new_product = $("<!-- 添加产品的div -->"
                    + "<div class='rp_productitem' style='margin-top:10px;'>"
                    + "    <!-- 产品图片 -->"
                    + "    <div class='rp_productitem_photo_div' >"
                    + "        <img class='rp_productitem_photo_img' src='../../Content/image/system/product/undefineproduct.jpg'>"
                    + "  </div>"
                    + "  <!-- /产品图片 -->"
                    + "  <!-- 产品信息 -->"
                    + "  <div class='rp_productitem_info_div'>"
                    + "      <ul class='rp_productitem_info_div_detailinfo'>"
                    + "          <li class='rp_productitem_info_div_detailinfo_li'>名称：<input type='text' name='username' class='rp_name' maxlength='50' placeholder='填写名称' value='" + product_name + "' />"
                    + "          </li>"
                    + "          <li class='rp_productitem_info_div_detailinfo_li'>库存：<input type='text' name='username' class='rp_sum' maxlength='50' placeholder='填写库存'  value='" + product_sum + "' />"
                    + "          </li>"
                    + "          <li class='rp_productitem_info_div_detailinfo_li'>单价：<input type='text' name='username' class='rp_price' maxlength='50' placeholder='填写单价'  value='" + product_price + "' />"
                    + "          </li>"
                    + "      </ul>"
                    + "      <ul>"
                    + "          <table class='rp_productitem_info_div_decript_table' cellpadding='0' cellspacing='0'  border='0'>"
                    + "              <tr>"
                    + "                  <td class='rp_productitem_info_div_decript_table_label'>"
                    + "                      <label>"
                    + "                          描述：</label><span class='product_manage_span' ><a class='product_avatar_a'></a><a class='product_recover_a'></a><a class='product_alter_a' ></a><a class='product_delete_a' ></a><span>"
                    + "                  </td>"
                    + "                  <!-- -->"
                    + "                  <td class='rp_productitem_info_div_decript_table_input'>"
                    + "                      <textarea cols='50' rows='10' class='rp_descript' maxlength='1024' placeholder='填写描述'  >" + product_decript + "</textarea>"
                    + "                      <input class='hide_productid_input' type='hidden'; value='" + msg.productid + "'/>"
                    + "                  </td>"
                    + "              </tr>"
                    + "          </table>"
                    + "      </ul>"
                    + "  </div>"
                    + "  <!-- /产品信息 -->"
                    + "</div></div>"
                    + "<!-- /添加产品的div -->");
                    $("#rp_productlist_div").prepend(new_product);
              
                }
                else {
                    alert("添加失败");
                }

            }
        });


    });
}


// 修改产品图片
function up_alterproduct_photo_click() {
    $(".product_avatar_a").die().live("click", function (e) {
        e.preventDefault();
//        alert(1);
    });
}

// 修改产品信息
function up_alterproduct_info_click() {
    $(".product_alter_a").die().live("click", function (e) {
        e.preventDefault();

        var parenthandler = $(this).parent().parent().parent().parent().parent().parent().parent();

        var product_id = parenthandler.find(".hide_productid_input").val();
        var product_name = parenthandler.find(".rp_name").val();
        var product_sum = parenthandler.find(".rp_sum").val();
        var product_price = parenthandler.find(".rp_price").val();
        var product_decript = parenthandler.find(".rp_descript").val();


        if (product_id == "") {
            alert("修改失败");
            return;
        }
        if (product_name == "") {
            parenthandler.find(".rp_name").focus();
            return;
        }
        if (product_sum == "") {
            parenthandler.find(".rp_sum").focus();
            return;
        }
        if (product_price == "") {
            parenthandler.find(".rp_price").focus();
            return;
        }
        if (product_decript == "") {
            parenthandler.find(".rp_descript").focus();
            return;
        }

        $.ajax({
            url: "/Mange_Main/AlterProduct",
            type: "post",
            cache: false,
            data: {
                product_id: product_id,
                product_name: product_name,
                product_sum: product_sum,
                product_price: product_price,
                product_decript: product_decript
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    alert(msg.Message);
                }
                else {
                    alert(msg.Message);
                }

            }
        });
    });
}

// 恢复产品信息
function up_recoverproduct_click() {
    $(".product_recover_a").die().live("click", function (e) {
        e.preventDefault();
        var parenthandler = $(this).parent().parent().parent().parent().parent().parent().parent();

        var product_id = parenthandler.find(".hide_productid_input").val();

        if (product_id == "") {
            alert("恢复失败");
            return;
        }
        $.ajax({
            url: "/Mange_Main/GetRemoverProduct",
            type: "post",
            cache: false,
            data: {
                product_id: product_id
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    parenthandler.find(".rp_name").val(msg.product_name);
                    parenthandler.find(".rp_sum").val(msg.product_sum);
                    parenthandler.find(".rp_price").val(msg.product_price);
                    parenthandler.find(".rp_descript").val(msg.product_decript);
                }
                else {
                    alert(msg.Message);
                }

            }
        });
    });
}

// 删除产品
function up_deleteproduct_click() {
    $(".product_delete_a").die().live("click", function (e) {
        e.preventDefault();
        var parenthandler = $(this).parent().parent().parent().parent().parent().parent().parent();

        var product_id = parenthandler.find(".hide_productid_input").val();

        if (product_id == "") {
            alert("删除失败");
            return;
        }

        var truthBeTold = window.confirm("确定删除?");
        if (truthBeTold) {
            $.ajax({
                url: "/Mange_Main/DeleteProduct",
                type: "post",
                cache: false,
                data: {
                    product_id: product_id
                },
                dataType: "json",
                success: function (msg) {
                    if (msg.Result) {
                        parenthandler.parent().remove();
                    }
                    else {
                        alert(msg.Message);
                    }
                }
            });
        }
    });
}